﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.Nutritionknowledge
{
    public partial class Storageintro : SecurityPage
    {
        T_RawMaterial tRawMaterial = new T_RawMaterial();   //原料操作
        T_IntroduceStorage tIntroduceStorage = new T_IntroduceStorage();//贮存知识

        public override string PrivCode()
        {
            return "9a9f6d9a1be2b151";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
            }
        }

        public void BindTreeView()
        {
            NutrientsTree.Nodes.Clear();
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.食品检测)).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.EToString(),
                    Target = "Parent",
                };
                childNode = GetRawByTypeId(childNode);
                NutrientsTree.Nodes.Add(childNode);
            });
            SysTree_SelectedNodeChanged(null, null);
        }

        private TreeNode GetRawByTypeId(TreeNode tn)
        {
            int Pid = int.Parse(tn.Value);
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0", Pid).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.ToString(),
                    Target = "Parent"
                };
                childNode = GetRawByTypeId(childNode);
                tn.ChildNodes.Add(childNode);
            });

            int TypeId = int.Parse(tn.Value);
            List<E_RawMaterial> rmlist = tRawMaterial.GetModelList("PeriodTypeStorKnowledgeID =" + TypeId);
            rmlist.ForEach(a =>
            {
                TreeNode rnod = new TreeNode()
                {
                    Text = a.RAWName,
                    Value = a.id.EToString(),
                    Target = "Child"
                };
                tn.ChildNodes.Add(rnod);
            });
            return tn;
        }

        protected void SysTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = this.NutrientsTree.SelectedNode;
            if (tn != null && tn.Target == "Child")
            {
                this.labRawName.Text = tn.Text;
                int RawID = int.Parse(tn.Value);
                E_IntroduceStorage eIntroduceStorage = tIntroduceStorage.GetModelList("RawID=" + RawID).FirstOrDefault();
                if (eIntroduceStorage != null)
                {
                    this.lab_RawContent.Text = eIntroduceStorage.RawContent;//原料简介
                    this.lab_StoragePrompt.Text = eIntroduceStorage.StoragePrompt;//贮存提示
                }
                else
                {
                    this.lab_RawContent.Text = "";//原料简介
                    this.lab_StoragePrompt.Text = "";//贮存提示
                }
                this.HidRawID.Value = RawID.ToString();
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Txt_Search.Text))
            {
                return;
            }
            for (int i = 0; i < NutrientsTree.Nodes.Count; i++)
            {
                for (int ii = 0; ii < NutrientsTree.Nodes[i].ChildNodes.Count; ii++)
                {
                    if (NutrientsTree.Nodes[i].ChildNodes[ii].Text.Contains(Txt_Search.Text))
                    {
                        TreeNode node = NutrientsTree.Nodes[i].ChildNodes[ii];
                        string text = node.Text;
                        node.Text = HttpUtility.HtmlDecode(node.Text).Replace(Txt_Search.Text, "<span class='Red'>" + Txt_Search.Text + "</span>");
                    }
                }
            }
        }
    }
}